package com.study.get.jsl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader.Feature;
import com.study.get.jsl.DataDTO.JslRowCell;
import com.study.get.jsl.DataDTO.KzzBlackListDTO;
import com.study.get.jsl.DataDTO.KzzDTO;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author zhuzi
 * 2024/6/14 18:6
 */
public class KzzDemo {

    @Test
    public void fores() {

        //设置时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //格式化当前时间
        String dateTime = LocalDateTime.now().format(formatter);
        System.out.println(dateTime);

        AtomicInteger atomicInteger = new AtomicInteger(1);
        for (int i = 0; i < 20; i++) {
            String no = StrUtil.fillBefore(String.valueOf(atomicInteger.getAndIncrement()), ' ', 2);
            System.out.println(no);

        }
    }

    /**
     * 可转债市值
     */
    @Test
    public void kzzMarket() {
        String body = FileUtil.readUtf8String("/Users/zhuzi/Downloads/kzz-24-06-14.json");
        List<JslRowCell> kzzDTOS = JSON.parseArray(JSON.parseObject(body).getString("rows"), JslRowCell.class, Feature.SupportSmartMatch);
        market(kzzDTOS, true);
        market(kzzDTOS, false);


        //System.out.println(s);
    }

    private static void market(List<JslRowCell> kzzDTOS, boolean filter) {
        List<KzzDTO> kzzList = kzzDTOS.stream().map(JslRowCell::getCell).peek(i -> i.calcCurrReal()).filter(ia -> !StringUtils.contains(ia.getBondNm(), "普利转债")).filter(i -> {
            if (filter) {
                return filter && Objects.nonNull(i.getYearLeft()) && NumberUtil.isGreater(i.getYearLeft(), new BigDecimal("0.4"));
            } else {
                return true;
            }
        }).sorted(Comparator.comparing(KzzDTO::getCurrReal)).limit(25).collect(Collectors.toList());

        AtomicInteger atomicInteger = new AtomicInteger(1);
        if (filter) {
            System.out.println("条件--剩余时间大于0.4年|");
        }
        System.out.println("  序号\t名称\t市值\t价格\t溢价率");
        kzzList.forEach(i -> {
            int andIncrement = atomicInteger.getAndIncrement();
            String format = StrUtil.format("{} {} {}亿 {}¥ |{}", StrUtil.fillBefore(String.valueOf(andIncrement), ' ', 2), i.getBondNm(), i.getCurrReal(), i.getPrice().setScale(3), i.getPremiumRt());
            System.out.println(format);
        });

        System.out.println(StrUtil.center("-", 20));
    }

    @Test
    public void heimingdan() {
        String filePaht = "/Users/zhuzi/Downloads/sql/转债黑名单.txt";

        List<String> readLines = FileUtil.readLines(filePaht, "UTF-8");
        List<String> collect = readLines.stream().filter(StrUtil::isNotBlank).map(i -> i.split(",")).flatMap(Arrays::stream).distinct().collect(Collectors.toList());
        String body = FileUtil.readUtf8String("/Users/zhuzi/Downloads/kzz-24-06-14.json");
        List<JslRowCell> kzzDTOS = JSON.parseArray(JSON.parseObject(body).getString("rows"), JslRowCell.class, Feature.SupportSmartMatch);
        List<KzzBlackListDTO> kzzList = kzzDTOS.stream().map(JslRowCell::getCell).filter(i -> !collect.contains(i.getBondNm())).peek(i -> i.calcCurrReal())
                //.filter(i -> NumberUtil.isGreater(i.getYearLeft(), new BigDecimal("0.4")))
                .sorted(Comparator.comparing(KzzDTO::getCurrReal)).map(i -> {
                    KzzBlackListDTO kzzBlackListDTO = new KzzBlackListDTO();
                    BeanUtils.copyProperties(i, kzzBlackListDTO);
                    return kzzBlackListDTO;

                }).collect(Collectors.toList());
        String templateFileName = "/Users/zhuzi/Downloads/非黑名单.xlsx";
        EasyExcel.write(templateFileName, KzzBlackListDTO.class).sheet("非黑名单").doWrite(kzzList);

    }
}
